package cn.hebutrun.oulabot.controller;

import cn.hebutrun.oulabot.entity.OulaFile;
import cn.hebutrun.oulabot.service.IOulaFileService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * oula文件表 前端控制器
 *
 * @author Adai
 * @since 2022-10-10
 */
@Api(tags = "文件接口")
@RestController
@RequestMapping("/oulaFile")
@RequiredArgsConstructor
public class OulaFileController {

  private final IOulaFileService iOulaFileService;

  @ApiOperation("上传文件")
  @PostMapping
  public boolean upload(@RequestParam MultipartFile[] files) {
    return iOulaFileService.upload(files);
  }

  @GetMapping
  @ApiOperation("获取单个文件")
  public OulaFile getOneFile(@RequestParam String fileId) {
    return iOulaFileService.getOne(
        Wrappers.lambdaQuery(OulaFile.class).eq(OulaFile::getId, fileId));
  }

  @GetMapping("/bytes")
  @ApiOperation("获取单个文件")
  public byte[] getOne(@RequestParam String fileId) {
    return iOulaFileService
        .getOne(Wrappers.lambdaQuery(OulaFile.class).eq(OulaFile::getId, fileId))
        .getFileData();
  }
}
